


package com.starmark.gateway.pt.provider.web;

import com.starmark.core.framework.base.AbstractController;
import com.starmark.core.framework.util.BeanMapper;
import com.starmark.gateway.pt.provider.entity.PtDataSource;
import com.starmark.gateway.pt.provider.service.IPtDataSourceService;
import com.starmark.gateway.pt.provider.vo.PtDataSourcePageVo;
import com.starmark.gateway.pt.provider.vo.PtDataSourceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 数据源 前端控制器
 * </p>
 *
 * @author starmark
 * @since 2020-05-24
 */
@RestController
@Api(value = "数据源管理", description = "数据源管理")
@RequestMapping("pt/data/source")
public class PtDataSourceController extends AbstractController {
    @Autowired
    IPtDataSourceService ptDataSourceService;

    @ApiOperation(value = "查询数据源列表")
    @PostMapping(value = "/query")
    public Object query(@RequestBody Map<String, Object> param) {

        return BeanMapper.mapPage(ptDataSourceService.query(param), PtDataSourcePageVo.class);
    }


    @ApiOperation(value = "测试数据源")
    @PostMapping(value = "/test")
    public Boolean test(@RequestBody PtDataSourceVo ptDataSourceVo) {
        PtDataSource ptDataSource = BeanMapper.map(ptDataSourceVo, PtDataSource.class);
        return ptDataSourceService.test(ptDataSource);
    }

    @ApiOperation(value = "新增数据源")
    @PostMapping
    public PtDataSourceVo add(@RequestBody PtDataSourceVo ptDataSourceVo) {

        PtDataSource ptDataSource = BeanMapper.map(ptDataSourceVo, PtDataSource.class);
        ptDataSourceService.save(ptDataSource);
        return BeanMapper.map(ptDataSource, PtDataSourceVo.class);
    }

    @ApiOperation(value = "数据源详情")
    @GetMapping(value = "/{id}")
    public PtDataSourceVo get(@PathVariable("id") String id) {
        PtDataSource ptDataSource = ptDataSourceService.getById(id);
        return BeanMapper.map(ptDataSource, PtDataSourceVo.class);
    }

    @PutMapping
    @ApiOperation(value = "修改数据源")
    public PtDataSourceVo update(@RequestBody PtDataSourceVo ptDataSourceVo) {
        PtDataSource ptDataSource = BeanMapper.map(ptDataSourceVo, PtDataSource.class);
        ptDataSourceService.updateById(ptDataSource);
        return BeanMapper.map(ptDataSource, PtDataSourceVo.class);
    }

    @DeleteMapping(value = "/{id}")
    @ApiOperation(value = "删除数据源")
    public void delete(@PathVariable("id") String id) {

        ptDataSourceService.removeById(id);
    }

}
